package com.example.redis.reflect;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.redis.Student;
import lombok.Data;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Reflects {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
        Student student=new Student();
        student.setName("yy");
        Field field = student.getClass().getDeclaredField("name");
        field.setAccessible(true);
        field.set(student,"qq");
        System.out.println(field.get(student));
        Method setName = student.getClass().getDeclaredMethod("setName", String.class);
        setName.setAccessible(true);
        setName.invoke(student,"ww");
        Method method = student.getClass().getDeclaredMethod("getName");
        method.setAccessible(true);
        System.out.println(method.invoke(student));
        Annotation[] annotations = student.getClass().getDeclaredAnnotations();
        for (Annotation annotation : annotations){
            if (annotation instanceof Data){
                Data data=(Data)annotation;
                System.out.println(StrUtil.format("{}",data.staticConstructor()));
            }
        }
        Student student1 = student.getClass().newInstance();
        System.out.println(student1.toString());
        System.out.println(ReflectUtil.newInstance(Student.class, "name", 13));
    }
}
